package com.geline.sqlbackup.util;

import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;

/**
 * 设置生成包目录格式
 * @author: 95cdoer
 * @date: 2018/12/5 0005 9:57
 */
@Slf4j
public class JavaPackageKit {

    public static String getTableEntityName(boolean useRemoveTableHead, boolean useCamelCase, String tableName){
        String entity = tableName.toLowerCase().trim();
        if(useRemoveTableHead){
            entity = entity.substring(entity.indexOf("_")+1);
        }
        if(useCamelCase){
            entity = StrUtil.toCamelCase(entity);
        }
        return StrUtil.upperFirst(entity.trim());// 无此行容易有bug --- sys_ dict_type ---> dictType.java
    }

    /**
     * ${packageName}.entity.${Entity}Entity.java  --->  ${Entity}Entity
     * ${packageName}.mapper.${Entity}Mapper.java  --->  ${Entity}Mapper
     * @param classFullName
     * @return
     */
    public static String getClassName(String classFullName, String entityName){
        if(StrUtil.isNotBlank(classFullName)){
            classFullName = classFullName.replaceAll("\\.java", "");
            classFullName = classFullName.substring(classFullName.lastIndexOf(".")+1);
        }
        if (classFullName != null && classFullName.indexOf("Entity") != -1) {
            return classFullName.replaceAll("\\$\\{Entity\\}", entityName);
        }else {
            return entityName;
        }
    }

    /**
     * ${packageName}.entity.${Entity}.java  --->  ${Entity}.java
     * mapper.${Entity}.xml --> ${Entity}.xml
     * mapper.${Entity}.hbm.xml --> ${Entity}.hbm.xml
     * @param fullName
     * @return
     */
    public static String getFileName(String fullName, String entityName){
        if(fullName.endsWith(".hbm.xml")){
            fullName = fullName.substring(0, fullName.length()-8);
            //classFullName = classFullName.replaceAll("\\.hbm.xml", "");
            fullName = fullName.substring(fullName.lastIndexOf(".")+1);
            if (fullName != null && fullName.indexOf("Entity") != -1) {
                return fullName.replaceAll("\\$\\{Entity\\}", entityName)+".hbm.xml";
            }else {
                return entityName+".hbm.xml";
            }
        }else if(fullName.endsWith(".xml")){
            fullName = fullName.substring(0, fullName.length()-4);
            //classFullName = classFullName.replaceAll("\\.xml", "");
            fullName = fullName.substring(fullName.lastIndexOf(".")+1);
            if (fullName != null && fullName.indexOf("Entity") != -1) {
                return fullName.replaceAll("\\$\\{Entity\\}", entityName)+".xml";
            }else {
                return entityName+".xml";
            }
        } else if(fullName.endsWith(".java")){
            fullName = fullName.substring(0, fullName.length()-5);
            //classFullName = classFullName.replaceAll("\\.xml", "");
            fullName = fullName.substring(fullName.lastIndexOf(".")+1);
            if (fullName != null && fullName.indexOf("Entity") != -1) {
                return fullName.replaceAll("\\$\\{Entity\\}", entityName)+".java";
            }else {
                return entityName+".java";
            }
        }
        return null;
    }

}
